package com.practice.J2E.thread;

/**
 * 酸奶店老板
 * @author kayn
 * @create 2023-09-21 下午 04:30
 */
public class YogurtSeller implements Runnable{

    private final Shop shop;

    private final int yogurtCount;

    public YogurtSeller(int yogurtCount, Shop shop) {

        this.shop = shop;

        this.yogurtCount = yogurtCount;

    }

    @Override
    public void run() {

        shop.startMaking();

        for (int i = 0; i < yogurtCount; i++) {

            try {

                Thread.sleep(1000);

                String yogurt = "酸奶" + i;

                synchronized (shop) {

                    shop.makeYogurt(yogurt);


                    if (i == yogurtCount - 1) {
                        shop.stopMaking();
                    }

                    shop.notify();

                }

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }


    }

}
